@echo off
start /b iisreset > nul
::************** Start batch ***************************************************
set SILENT=TRUE
set BUILD_MODE=Release
rem set BUILD_MODE=Debug
set BUILD_PLAT="Any CPU"
set TOOL=MSBuild
rem set TOOL=VS90
set BUILD_TYPE=build
Set CURRENTDIR=%CD%\bin\x86\Release

if not "%1"=="" set SILENT=%1
if not "%2"=="" set BUILD_MODE=%2
if not "%3"=="" set BUILD_PLAT=%3
if not "%4"=="" set TOOL=%4

call :PrepareEnvironment

::************** Solutions ***************************************************

call :BuildSolution CSLib.Net "Any CPU"

Set CURRENTDIR=%CD%\bin\Windows Phone\Release
call :BuildSolution CSLib.Net "Windows Phone"

Set CURRENTDIR=%CD%\bin\Android\Release
call :BuildSolution CSLib.Net "Android"

::************** End batch ***************************************************
goto EXIT_BATCH

:PrepareEnvironment
if "%TOOL%" == "MSBuild" call :PrepareEnvironmentMSBuild
if "%TOOL%" == "VS90" call ::PrepareEnvironment90
goto :eof

:PrepareEnvironmentMSBuild

set MSBUILD=%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
if not exist "%MSBUILD%" set MSBUILD=%WINDIR%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
if not exist "%MSBUILD%" echo Wrong Path for MSBUILD[%MSBUILD%] && exit 0

goto :eof

:PrepareEnvironment90

set VSIDE90=%VS90COMNTOOLS%..\IDE\devenv.exe
if not exist "%VSIDE90%" set VSIDE90=%programfiles%\Microsoft Visual Studio 10\Common7\IDE\devenv.exe
if not exist "%VSIDE90%" echo Wrong Path for VSIDE90[%VSIDE90%] && exit 0

goto :eof

:BuildSolution
if "%TOOL%" == "MSBuild" call :BuildSolutionMSBuild %1 %2
if "%TOOL%" == "VS90" call ::BuildSolution90 %1 %2
goto :eof

:BuildSolutionMSBuild
:: %1 is the solution name without extension
:: %2 is the platform definition
if "%SILENT%"=="TRUE" set CONSOLELOG=/consoleloggerparameters:Summary;ErrorsOnly
if exist build-%1.log del /q build-%1.log
echo.
echo ********** Start build %1 at %time% **********
echo.
set target=
if("%BUILD_TYPE%" == "rebuild") set target=/t:Rebuild
"%MSBUILD%" %target% /maxcpucount:2 %1.sln /p:Configuration=%BUILD_MODE%;Platform=%2;outputpath="%CURRENTDIR%" /fl /flp:LogFile=build-%1.log %CONSOLELOG% /nologo
if not "%errorlevel%"=="0" echo Error on building %1.sln at %time% && start build-%1.log && pause && exit 0
if "%SILENT%"=="TRUE" call :ReadLastLineFromFile "build-%1.log" 4
goto :eof

:BuildSolution90
:: %1 is the solution name without extension
:: %2 is the platform definition
if exist build-%1.log del /q build-%1.log
echo. 
echo ********** Start build %1 at %time% **********
echo. 
"%VSIDE90%" /%BUILD_TYPE% "%BUIL_MODE%|%2" %1.sln /out build-%1.log
if not "%errorlevel%"=="0" echo Error on building %1.sln at %time% && start build-%1.log && pause && exit 0
call :ReadLastLineFromFile "build-%1.log" 1

goto :eof

:ReadLastLineFromFile 
:: %1 is the filename
::Determine the line number of the next to last line
For /F %%A In ('Find /C /V "" ^< %1') Do Set /A _ = %%A - %2
::Read last line
More /E +%_% %1
::return  
echo.
goto :eof

:EXIT_BATCH
echo Batch Finished at %time%
echo 
pause